<link rel="stylesheet" href="../../layui/css/layui.css"  media="all">
<script src="../../layui/layui.js?t=1531663423583" charset="utf-8"></script>
<script src="https://lib.baomitu.com/jquery/3.3.1/jquery.min.js"></script>
<div style="padding: 10px">
<div class="layui-form" >
	<div class="layui-form-item">
		<label class="layui-form-label">商品名称</label>
		<div class="layui-input-block">
			<input type="text" id="shopname" placeholder="请输入商品名称" autocomplete="off" class="layui-input">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">商品分类</label>
		<div class="layui-input-block">
			<select id="shoptype">
				<option value="">请选择商品分类</option>
			</select>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">商品单价</label>
		<div class="layui-input-block">
			<input type="number" id="money" placeholder="请输入商品单价" autocomplete="off" class="layui-input">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">商品销量</label>
		<div class="layui-input-block">
			<input type="number" id="xiaoliang" placeholder="请输入商品销量" autocomplete="off" class="layui-input">
		</div>
	</div>


	<div class="layui-form-item">
		<label class="layui-form-label">商品描述</label>
		<div class="layui-input-block">
			<textarea id="shoptext" style="display: none;"></textarea>
		</div>
	</div>


</div>

<div style="text-align: right">
	<button class="layui-btn" lay-submit lay-filter="formDemo" onclick="sub()">保存</button>

</div>

</div>

<script>
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return decodeURI(r[2]);
        return null;
    }

    function sub() {
        var shoptype =$('#shoptype').val();
        if (shoptype == ""){
            layer.msg("请选择商品分类");
            return;
        }
		var typename = $("#shoptype").find("option:selected").text();
		if (typename == ""){
			layer.msg("请选择商品分类");
			return;
		}

		var shopname =$('#shopname').val();
        if (shopname == ""){
            layer.msg("请输入商品名称");
            return;
        }

        var money =$('#money').val();
        if (money == ""){
            layer.msg("请输入商品单价");
            return;
        }
		var xiaoliang =$('#xiaoliang').val();
		if (xiaoliang == ""){
			layer.msg("请输入商品销量");
			return;
		}

        layedit.sync(layeditindex);
        var shoptext =$('#shoptext').val();

        if (shoptext == ""){
            layer.msg("请输入商品描述");
            return;
        }


		var post = "id="+getQueryString("id")+"&shoptype="+shoptype+"&xiaoliang="+xiaoliang+"&typename="+typename+"&shopname="+shopname+"&money="+money+"&shoptext="+shoptext;
        $.post("../../admin/editShop",post,function (data) {
            layer.msg("操作成功");
            setTimeout("parent.layer.close(index);",1000);
        });
    }


    var layedit,layeditindex;
    layui.use(['layedit',"form"], function(){
        layedit = layui.layedit;


        $.post("../../admin/getShoptype",function (data) {
            for (var i = 0;i<data.data.length;i++){
                $("#shoptype").append('<option value="'+data.data[i].id+'">'+data.data[i].name+'</option>');
            }
            layui.form.render();


            $.post("../../admin/getShopByid","id="+getQueryString("id"),function (data) {

                $('#shoptype').val(data.data.typeid);
				$('#xiaoliang').val(data.data.xiaoliang);


                $('#shopname').val(data.data.shopname);
                $('#money').val(data.data.money);

                $('#shoptext').val(data.data.shoptext);

                layeditindex = layedit.build('shoptext',{
                    tool: [
                        'strong' //加粗
                        ,'italic' //斜体
                        ,'underline' //下划线
                        ,'del' //删除线

                        ,'|' //分割线

                        ,'left' //左对齐
                        ,'center' //居中对齐
                        ,'right' //右对齐
                        ,'link' //超链接
                        ,'unlink' //清除链接
                        ,'face' //表情
                    ]
                }); //建立编辑器
                layui.form.render();

            });

        });
    });

</script>

